<script>
  import { MasterInfoApi, SystemApi } from './api';
  
  export default {
    onLaunch: function() {
      console.log('App Launch', uni.getWindowInfo())
      this.globalData.safeAreaTop = uni.getWindowInfo().statusBarHeight;
      this.checkLogin();
    },
    onShow: function() {
      console.log('App Show')
    },
    onHide: function() {
      console.log('App Hide')
    },
    methods: {
      checkLogin() {
        const token = uni.getStorageSync('token') || null;
        if (token) {
          return true;
        }
        uni.clearStorageSync();
        uni.reLaunch({
          url: '/pages/login/login'
        });
        return false;
      },
      getUserInfo(callback) {
        MasterInfoApi().then((res) => {
          uni.setStorageSync('userInfo', res.data)
          typeof callback === 'function' && callback(res.data)
        }).catch(() => {
          typeof callback === 'function' && callback(uni.getStorageSync('userInfo'))
        })
      },
      getSystem(callback) {
        SystemApi().then((res) => {
          typeof callback === 'function' && callback(res.data)
        }).catch(() => {})
      },
      getLast() {
        return new Promise((resolve, reject) => {
          setTimeout(() => {
            const pages = getCurrentPages()
            const active = pages[pages.length - 1]
            resolve(active)
          }, 300)
        })
      }
    },
    globalData: {
      // 安全区域
      safeAreaTop: 0,
    }
  }
</script>

<style lang="scss">
  /*每个页面公共css */
  page {
    background-color: $uni-bg-color-grey;
  }
</style>
